<?php

/**
 * IndexController class
 */
class Admin_IndexController extends Zend_Controller_Action
{

	public function indexAction()
	{
	}

	public function profileAction()
	{
		$table = new Default_Model_User();
		$user = Zend_Auth::getInstance()->getIdentity();
		$row = $table->fetchRow(array('user_id = ?' => $user['user_id']));
		if ($row)
		{
			$form = new Admin_Form_User();
			$form->removeElement('role');
			$form->removeElement('is_active');
			$form->removeElement('username');
			$form->load($row);
			if (
				$this->_request->isPost() &&
				$form->isValid($this->_request->getPost())
			)
			{
				$data = $form->getValues();
				$row->setFromArray($data);
				if ($row->save())
				{
					Zend_Auth::getInstance()->getStorage()->write($row->toArray());
					$this->_helper->flashMessenger('success.edit');
					$this->_helper->redirector->gotoSimple();
				}
			}
			$this->view->form = $form;
		}
	}

	public function loginAction()
	{
		$form = new Admin_Form_Login();
		if (
			$this->_request->isPost() &&
			$form->isValid($this->_request->getPost())
		)
		{
			$table = new Default_Model_User();
			$row = $table->fetchRow(array(
				'username = ?' => $form->getValue('username', ''),
				'userpass = ?' => sha1($form->getValue('userpass', '')),
				'is_active = 1',
			));
			if ($row)
			{
				Zend_Auth::getInstance()->getStorage()->write($row->toArray());
				$this->_helper->redirector->gotoRoute();
			}
		}
		$this->view->form = $form;
		$this->_helper->layout()->disableLayout();
	}

	public function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		$this->_helper->redirector->gotoSimple('index', 'index');
	}

	public function pingAction()
	{
		$this->_helper->json(time());
	}

	public function infoAction()
	{
		$this->_helper->viewRenderer->setNeverRender();
		$this->_helper->layout->disableLayout();
		phpinfo();
	}

}
